Open Hashing using ‘C

Open hashing, otherwise called separate tying, is an impact goal method utilized in hash tables. Hash tables are information structures that store key-esteem coordinates and give proficient query, addition, and erasure tasks. Hash capabilities are utilized to plan keys to cluster lists, taking into consideration fast admittance to values.


In open hashing, each space or pail in the hash table contains a connected rundown or another type of holder. At the point when a crash happens, meaning at least two keys are planned to a similar record, the impacted keys are put away in a similar space utilizing the holder. This permits numerous keys to be put away at a similar file, framing a chain of components.


While looking for a particular key, the hash capability is applied to the key, and the subsequent record is utilized to find the relating space in the hash table. Then, the connected rundown or compartment at that opening is crossed to find the ideal key-esteem pair. The intricacy of the pursuit activity in open hashing is relative to the length of the chain, which in a perfect world ought to be short.


In the event that another key slams into a current key during an addition, it is added to the chain in the relating space. This should be possible by attaching the new key-esteem pair to the furthest limit of the connected rundown or utilizing some other reasonable compartment.


Open hashing requires extra memory to store the connected records or compartments, and the exhibition of the hash table relies upon the heap factor, which is the typical number of keys per space. In the event that the heap factor turns out to be too high, the chains can turn out to be long, bringing about corrupted execution. In this manner, it is vital to screen and change the size of the hash table to keep an ideal burden factor.


In general, open hashing is an adaptable impact goal procedure that handles crashes by putting away impacted keys in discrete chains inside the hash table. It is broadly utilized in different programming dialects and applications where effective key-esteem query is required.

Program:-

#include<stdio.h>

#include<stdlib.h>

#define size 10

 

struct node{

int data;

struct node *next;

};

 

struct node *chain[size];

void openhash()

{

//struct node *chain[size];

int i;

for(i=0;i<size;i++)

 chain[i]=NULL;

 

}

 

void insert(int a)

{

 

struct node *c;

c=(struct node*)malloc(sizeof(struct node));

c->data=a;

c->next=NULL;

int key =a % size;

if(chain[key]==NULL){

chain[key]=c;

 

}

else

{

struct node *temp=chain[key];

while(temp->next!=NULL)

{

temp=temp->next;

}

temp->next=c;

}

}

 

void print()

{

 

int i;

for(i=0;i<size;i++)

{

struct node*temp=chain[i];

printf("chain[%d]-->",i);

while(temp!=NULL)

{

printf("%d-->",temp->data);

temp=temp->next;

}

printf("NULl\n");

}

}

 

int main()

{

openhash();

int i,a;

for(i=0;i<6;i++)

{

printf("enter number:");

scanf("%d",&a);

insert(a);

}

print();

}

 

Output:-